/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.stock;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */

@ManagedBean(name="listStock")
@SessionScoped

public class Liststock implements Serializable{
    
   @Resource(name="jdbc/antonsDatasource") // data source to use for mysql queries
    
   private DataSource ds;
   
    public StockEntity getStock() {
        return stock;
    }

    public void setStock(StockEntity stock) {
        this.stock = stock;
    }
    
   ArrayList<StockEntity> list;
   
    StockEntity stock;
    private int itemID;
    private String itemName;
    private String itemDescription;
    private String itemRarity;
    private int itemStock;
    private int rarity =0;
    private int selectedValue;
    

    
    public int getSelectedValue() {
        return selectedValue;
    }

    

    public int getItemID() {
        return itemID;
    }

    public void setItemID(int itemID) {
        this.itemID = itemID;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getItemRarity() {
        return itemRarity;
    }

    public void setItemRarity(String itemRarity) {
        this.itemRarity = itemRarity;
    }

    public int getItemStock() {
        return itemStock;
    }

    public void setItemStock(int itemStock) {
        this.itemStock = itemStock;
    }
   
    
    @ManagedProperty(value="#{destroyStock}")
    
    private DestroyStock destroyStock;

    public DestroyStock getDestroyStock() {
        return destroyStock;
    }

    public void setDestroyStock(DestroyStock destroyStock) {
        this.destroyStock = destroyStock;
    }
    
   
     public List<StockEntity> getDinnerIngredients() throws SQLException{
                    
       
		if(ds==null)
			throw new SQLException("Can't get data source");// throws exception if datasource not available
       try (Connection con = ds.getConnection()) {
           if(con==null)
               throw new SQLException("Can't get database connection");
           PreparedStatement ps
                   = con.prepareStatement(
                           "Select item_ID ,item_name ,item_description ,item_rarity, item_stock  from Stock"); // query select from stock specified attributes
           
           //get  item info from database
           ResultSet result =  ps.executeQuery();
           
           list = new ArrayList<>();
           
           while(result.next()){
               
               stock=new StockEntity();
               
               
               stock.setItemID(result.getInt("item_ID"));
               stock.setItemName(result.getString("item_name"));
               stock.setItemDescription(result.getString("item_description"));
               stock.setItemRarity(result.getInt("item_rarity"));
               stock.setItemStock(result.getInt("item_stock"));
               
               //adds ingredients to list
               list.add(stock);
           }
       }
		return list;
                
	}// ends method
    
     
     public String prepareUpdate(StockEntity stockEntity)
             
     {       String target = "/admin/stock/updatestock?faces-redirect=true";
     

             this.itemID=stockEntity.getItemID();
             this.itemName = stockEntity.getItemName();
             this.itemDescription =stockEntity.getItemDescription();
             this.itemRarity= Integer.toString(stockEntity.getItemRarity());
             this.itemStock= stockEntity.getItemStock();
             this.selectedValue=stockEntity.getItemID();
             return target;
             
       }
     
     
 

      public String niceView(int itemId) throws SQLException
            
    {  
        String sql;
        
        String target = "/admin/stock/view?faces-redirect=true";
        
        // select one matching dinner, dinner_name primary key in mysql table
        sql="Select item_ID ,item_name ,item_description ,item_rarity,item_stock  from Stock where item_id="+itemId+"";
        
        try (Connection con = ds.getConnection()) 
        {
            PreparedStatement ps; 
            ps = con.prepareStatement(sql );
            
            ResultSet result =  ps.executeQuery();
            
            while(result.next())
            {
                
                
                itemID = result.getInt("item_ID");
                itemName = result.getString("item_name");
                itemDescription = result.getString("item_description");
                itemRarity =(result.getString("item_rarity"));
                if(itemRarity.equals("1"))
                    itemRarity="Ja";
                else
                   itemRarity= "Nej";
                itemStock =(result.getInt("Item_stock"));
            }
        }
        return target;
    
    }
      
      
      
      
      
      
      
    
      
       public  String upDate( ) throws SQLException // Method used to update dinner
    {   
        String sql;
        String target;
        
         
        if(ds==null)
			throw new SQLException("Can't get data source");// throws exception if database not available
       
        
        try (Connection con = ds.getConnection()) {
            
            if(con==null)
                throw new SQLException("Can't get database connection");
            
           
           target = "/admin/updatedinner?faces-redirect=true";
            
         
            
            //item_ID | item_name   | item_description | item_rarity | item_stock
         
                sql="UPDATE Stock set item_id = ?, item_name=?, item_description=?, item_rarity=?,  item_stock=? where item_id ='"+selectedValue+"'" ;
                try (PreparedStatement ps = con.prepareStatement(sql ))
                {    
                    ps.setInt(1, itemID);
                    ps.setString(2,itemName);
                    ps.setString(3, itemDescription);
                    ps.setString(4, itemRarity);
                    ps.setInt(5, itemStock);  
                    ps.executeUpdate();
                    
                }
                           
                    
                }
                  return null;
            }
      
      
    
}
